﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using GMap.NET;

namespace BioImg
{
    public class BioImgProvider : BioImgProviderBase
    {
        public static readonly BioImgProvider Instance;

        readonly Guid id = new Guid("EF3DD303-3F74-4938-BF40-232D0595EE89");
        public override Guid Id
        {
            get
            {
                return id;
            }
        }

        readonly string name = "BioImg";
        public override string Name
        {
            get
            {
                return name;
            }
        }

        static BioImgProvider()
        {
            Instance = new BioImgProvider();
            InitImg();
            Instance.MaxZoom = (int)maxLevel;
        }
        public static int sampleId = -1;

        public static void InitImg()
        {
            string folder = @"D:\BioScan\液基-2019-12-12-13-12-33-16_198265";

            try
            {
                int rtn = ImageEngine.openslide_open(folder, ref sampleId);
                if (rtn < 0)
                {
                    //Log($"SlideInit ImageViewer_Init failed, sample: {sampleId}, rtn: {rtn}");
                    return;
                }
                rtn = ImageEngine.openslide_init(sampleId);
                if (rtn < 0)
                {
                    //Log($"SlideInit ImageViewer_Init failed, sample: {sampleId}, rtn: {rtn}");
                    return;
                }
                int bufferedWidth = 0;
                int bufferedHeight = 0;

                rtn = ImageEngine.openslide_get_level0_dimensions(sampleId, ref bufferedWidth, ref bufferedHeight);
                if (rtn < 0)
                {
                    //Log($"SlideInit ImageViewer_Init failed, sample: {sampleId}, rtn: {rtn}");
                    return;
                }
                rtn = ImageEngine.openslide_get_level_count(sampleId);
                double max;
                if (bufferedWidth > bufferedHeight)
                {
                    max = bufferedWidth / 256;
                }
                else
                {
                    max = bufferedHeight / 256;
                }

                maxLevel = Math.Ceiling(Math.Log(max) / Math.Log(2)) - 1;
                //MaxZoom = maxLevel;
                //options.tileSize = 256;


            }
            catch (Exception ex)
            {

            }
        }

        public static double maxLevel = 8;

        //private string getImagePath(int row, int col, int layer)
        //{
        //    string imgName = string.Format(format, folder, layer.ToString("00"), row.ToString("000"), col.ToString("000"));
        //    return imgName;
        //}
        private static void Log(string str)
        {

        }
        internal static PureImageProxy TileImageProxy;

        [HandleProcessCorruptedStateExceptions]
        [SecurityCritical]
        public override PureImage GetTileImage(GPoint pos, int level)
        {
            try
            {
                if (level > maxLevel)
                {
                    return null;
                }
                //PureImage a = new PureImage();

                //string url = MakeTileImageUrl(pos, zoom);
                // return GetTileImageUsingHttp(url);
                byte[] imagebyte = new byte[256 * 256 * 3];
                int[] size = new int[1];//
                size[0] = 256 * 256 * 3;
                System.Diagnostics.Debug.WriteLine("level:" + level + "x:" + (int)pos.X + "Y:" + (int)pos.Y);
                int rtn = ImageEngine.GetImageTile(level, (int)pos.X, (int)pos.Y, 256, imagebyte, size, sampleId);

                //if (TileImageProxy == null)
                //{
                //    TileImageProxy = new PureImageProxy();
                //}
                return GetTileImageFromArray(imagebyte);
                // return TileImageProxy.FromArray(imagebyte);
                //return null;
            }
            catch (System.AccessViolationException ex) //捕获cse类型的异常
            {
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }



        }


        #region GetTileImage

        public  PureImage GetTileImage22(GPoint pos, int level)
        {
            try
            {
                //PureImage a = new PureImage();

                string url = MakeTileImageUrl(pos, level);
                return GetTileImageUsingHttp(url);
            }
            catch
            {
                return null;
            }
        }
        #endregion


        string MakeTileImageUrl(GPoint pos, int zoom)
        {
            return string.Format(UrlFormat, zoom, pos.X, pos.Y);
        }

          static readonly string UrlFormat = @"http://bnv201900055513.slide.bionovationimc.com:8080/slidetile?path=D:/BioScan/2019-12-30-16-37-58-2-24_945135&level={0}&x={1}&y={2}";
        //@"http://bnv20190005544.slide.bionovationimc.com:8080/slidetile?path=D:\BioSlides\液基-2019-12-12-13-12-33-16_198265&level={0}&x={1}&y={2}";//aigo2.slide.bionovationimc.com

    }
}
